package com.jh.netty.client;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class TcpClient {

    public static void main(String[] args) throws IOException {

        try (Socket socket = new Socket("localhost", 8080); DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {

            Scanner scanner = new Scanner(System.in);

            while (scanner.hasNextLine()) {
                String msg = scanner.nextLine();
                byte[] outputBytes1 = msg.getBytes(StandardCharsets.UTF_8);
                out.writeInt(outputBytes1.length); // write header
                out.write(outputBytes1); // write body
                out.flush();
            }

//            // 请求服务器
//            String data1 = "牛顿";
//            byte[] outputBytes1 = data1.getBytes(StandardCharsets.UTF_8);
//            out.writeInt(outputBytes1.length); // write header
//            out.write(outputBytes1); // write body
//
//            String data2 = "爱因斯坦";
//            byte[] outputBytes2 = data2.getBytes(StandardCharsets.UTF_8);
//            out.writeInt(outputBytes2.length); // write header
//            out.write(outputBytes2); // write body
//
//            out.flush();

        }
        // 关闭连接
    }
}